package com.ybkj.daijia.api.member.v1;


import com.ybkj.daijia.server.service.DriverService;
import com.ybkj.daijia.server.service.MemberAccountService;
import com.ybkj.daijia.server.service.MemberService;
import com.ybkj.daijia.server.service.OrderService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

/**
 * 司机登录相关
 *
 * @author bo
 */
@Controller
@RequestMapping(value = "member/api/rest/v1")
public class MemberAccountController {

    private static final Logger logger = LoggerFactory.getLogger(MemberAccountController.class);

    @Autowired
    private MemberService memberService;
    @Autowired
    private MemberAccountService memberAccountService;
    @Autowired
    private DriverService driverService;
    @Autowired
    private OrderService orderService;

    /**
     * 获取验证码
     * @param phone 手机号
     * @return
     */
//	@RequestMapping(value="getCode",method=RequestMethod.POST)
//	public @ResponseBody ResultTransport getCode(String phone) {
//		
//		logger.debug("recv phone:{}",phone);
//		
//		if(StringUtils.isBlank(phone)){
//			return ResultTransport.getErrorBy(ErrorConstant.PHONE_NULL_ERROR);
//		}
//		
//		try {
//			memberService.getCode(phone,"APP");
//			
//			ResultTransport instance = ResultTransport.getErrorBy(ErrorConstant.SUCCESS);
//			
//			return instance;
//			
//		} catch (Exception e) {
//			logger.error("api v1 member getCode error: {}",e);
//			return ResultTransport.getErrorBy(ErrorConstant.SERVER_ERROR);
//		}
//		
//	}

    /**
     * 用户登录
     *
     * @param phone 手机号
     * @param code 验证码
     * @param appversion 版本号
     * @param appKey
     * @param token token令牌
     * @param timestamp 时间戳
     * @return
     */
//	@RequestMapping(value="login",method=RequestMethod.POST)
//	public @ResponseBody ResultTransport login(String phone,String code,String appversion,String appKey,String token,String timestamp) {
//		
//		logger.debug("recv phone:{},vcode:{},appversion{},appKey:{},token:{},timestamp:{}",phone,code,appversion,appKey,token,timestamp);
//		
//		if(StringUtils.isBlank(phone) || StringUtils.isBlank(code) || StringUtils.isBlank(appversion)  || SecurityUtils.isBlank(appKey,token, timestamp)) {
//			return ResultTransport.getErrorBy(ErrorConstant.PARAM_ERROR);
//		}
//		
//		if(!StringUtils.isNumeric(timestamp) || !SecurityUtils.checkTimestamp(Long.parseLong(timestamp))) {
//			return ResultTransport.getErrorBy(ErrorConstant.TIMESTAMP_ERROR);
//		}
//		
//		List<String> params = new LinkedList<String>();
//		params.add(phone);
//		params.add(code);
//		params.add(appversion);
//		params.add(timestamp);
//		params.add(appKey);
//		
//		if(!SecurityUtils.checkToken(params, token)) {
//			return ResultTransport.getErrorBy(ErrorConstant.TOKEN_ERROR);
//		}
//		
//		try {
//			MemberAccount account = memberAccountService.findByPhone(phone);
//			
//			if(null == account) {
//				return ResultTransport.getErrorBy(ErrorConstant.MEMBER_NOT_EXISTS_ERROR);
//			}
//			
//			Member member = memberService.findOne(account.getMemberId());
//			
//			if(!account.getVcode().equals(code)){
//				return ResultTransport.getErrorBy(ErrorConstant.CODE_ERROR);
//			}
//			
//			account.setVcode(null);
//			account.setExpired(null);
//			memberAccountService.updateByPrimaryKey(account);
//			
//			member.setAppVersion(appversion);
//			memberService.updateByPrimaryKey(member);
//			
//			ResultTransport instance = ResultTransport.getErrorBy(ErrorConstant.SUCCESS);
//			instance.setData(member);
//			
//			return instance;
//			
//		} catch (Exception e) {
//			logger.error("api v1 member login error: {}",e);
//			return ResultTransport.getErrorBy(ErrorConstant.SERVER_ERROR);
//		}
//		
//	}

//	@RequestMapping(value="memberInfo",method=RequestMethod.POST)
//	public @ResponseBody ResultTransport login(String phone,
//			String appKey,String token,String timestamp) {
//		
//		logger.debug("recv phone:{},appKey:{},token:{},timestamp:{}",phone,appKey,token,timestamp);
//		
//		if(StringUtils.isBlank(phone) || SecurityUtils.isBlank(appKey,token, timestamp)) {
//			return ResultTransport.getErrorBy(ErrorConstant.PARAM_ERROR);
//		}
//		
//		if(!StringUtils.isNumeric(timestamp) || !SecurityUtils.checkTimestamp(Long.parseLong(timestamp))) {
//			return ResultTransport.getErrorBy(ErrorConstant.TIMESTAMP_ERROR);
//		}
//		
//		List<String> params = new LinkedList<String>();
//		params.add(phone);
//		params.add(timestamp);
//		params.add(appKey);
//		
//		if(!SecurityUtils.checkToken(params, token)) {
//			return ResultTransport.getErrorBy(ErrorConstant.TOKEN_ERROR);
//		}
//		
//		try {
//			MemberAccount account = memberAccountService.findByPhone(phone);
//			
//			if(null == account || null==account.getMemberId()) {
//				return ResultTransport.getErrorBy(ErrorConstant.MEMBER_NOT_EXISTS_ERROR);
//			}
//			
//			ResultTransport instance = ResultTransport.getErrorBy(ErrorConstant.SUCCESS);
//			instance.setData(memberService.findOne(account.getMemberId()));
//			
//			return instance;
//			
//		} catch (Exception e) {
//			logger.error("api v1 member memberInfo error: {}",e);
//			return ResultTransport.getErrorBy(ErrorConstant.SERVER_ERROR);
//		}
//		
//	}
}
